package com.inspect;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

import com.inspect.dbConn.dbSingleton;
import com.inspect.log.LogSingleton;
import com.inspect.quartz.JobSchedule;
import com.inspect.util.Constant;

public class poller {

	private static Logger log = LogSingleton.get_logger(poller.class);

	public void do_poller(JobSchedule jobSchedule) throws Exception {
		log.info("系统启动，开始加载任务");
		dbSingleton db = dbSingleton.get_instance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (db == null) {
			log.error("oracle connect fail. schedule will exit");
			return;
		}
		/* get all task item */
		ArrayList<HashMap<String, String>> task_item = new ArrayList<HashMap<String, String>>();

		task_item = db.db_query_assoc(
				"select h.*,g.default_script as private_threshold, k.start_time from (select e.plan_id || '_' || d.script_id || '_' || a.asset_code as job_id,"
						+ "e.plan_id,a.asset_code,d.script_id,a.host_ip,a.uname,a.pwd,a.port,a.instance_name,a.zone,d.script_name,d.default_script,e.cron_expression from t_inspect_asset a,t_inspect_plan_templet_rp   b,t_inspect_templet_script_rp c,t_inspect_script d,t_inspect_plan  e where b.asset_code = a.asset_code and c.templet_id = b.templet_id and d.script_id = c.script_id and e.plan_id = b.plan_id and e.plan_status = '0' ) h"
						+ " left join t_inspect_threshold g on g.script_id = h.script_id  and g.asset_code = h.asset_code left join t_inspect_cornexpress_tmp k on k.plan_id = h.plan_id");
		if (task_item == null || task_item.size() == 0) {
			log.info("暂时没有任务可以被执行");
			return;
		}
//		LoadAppXml load = LoadAppXml.getInstance();
//
//		HashMap<String, String> config = load.getConfig();
//		String defaultZone = "";
//		if (!config.isEmpty()) {
//			defaultZone = config.get("inspect_defaultZone") == null ? "" : config.get("inspect_defaultZone");
//		}
//		String pactagePath = "com.inspect.packages.";// 可以弄成配置文件，配置脚本目录
//		if (defaultZone == null || defaultZone.equals("")) {
			log.info("任务总数：" + task_item.size());
			Iterator<HashMap<String, String>> it = task_item.iterator();
			while (it.hasNext()) {
				Map<String, String> temp = new HashMap<String, String>();
				temp = it.next();
				String id = temp.get(Constant.JOB_ID);
				String corn_pattern = temp.get(Constant.CRON_EXPRESSION);
				String start_time = sdf.format(new Date());
				// String start_time = temp.get(Constant.START_TIME);
				if (corn_pattern == null || "".equals(corn_pattern)) {
					log.error("查询结果有误——轮训时间");
					continue;
				}
//				String className = temp.get(Constant.SCRIPT_NAME);
				Object c = Class.forName("com.inspect.protocol.Inspect_sz").newInstance();
				// Inspect_aix_lvStatus c = new Inspect_aix_lvStatus();
				jobSchedule.addJob(id, Constant.defailtJobGroup, id, Constant.defaultTriggerGroup, c.getClass(),
						corn_pattern, sdf.parse(start_time), temp);
			}
//		} else {
//			log.info(defaultZone + "区任务总数：" + task_item.size());
//			List zones = (List) Arrays.asList(defaultZone.split(","));
//			Iterator<HashMap<String, String>> it = task_item.iterator();
//			while (it.hasNext()) {
//				Map<String, String> temp = new HashMap();
//				temp = (Map) it.next();
//				String id = (String) temp.get("JOB_ID");
//				String corn_pattern = (String) temp.get("CRON_EXPRESSION");
//				String className = (String) temp.get("SCRIPT_NAME");
//				String zone = (String) temp.get("ZONE");
//				if (zone != null && !"".equals(zone)) {
//					if (zones.contains(zone)) {
//
//						String start_time = sdf.format(new Date());
//
//						if ((corn_pattern == null) || ("".equals(corn_pattern))) {
//							log.error("查询结果有误——轮训时间");
//							continue;
//
//						}
//						log.info("任务(" + id + ")加载成功！");
//
//						Object c = Class.forName(pactagePath + className).newInstance();
//						jobSchedule.addJob(id, "inspectJob", id, "inspectTrigger", c.getClass(), corn_pattern,
//								sdf.parse(start_time), temp);
//					} else {
//						log.error("资产配置区域不在程序配置中，不加载任务(" + id + ")");
//					}
//				} else {
//					log.error("资产没有被分区或配置中没有填写默认分区,任务(" + id + ")");
//				}
//			}
//		}
	}

	public static void main(String[] args) {
		try {
			JobSchedule jobSchedule = new JobSchedule();
			jobSchedule.startScheduler();
			new poller().do_poller(jobSchedule);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}